#include <stdio.h>
#include <stdlib.h>

#define MAX_N 7000
#define MAX_M 300000

typedef struct s_edge
{
	int p;
	int k;
	int w;
} edge;

struct s_vertex
{
	struct s_vertex *n; //next
	int v; //vertex
};

typedef struct s_vertex vertex;

typedef struct s_list
{
	vertex *h; //head
	vertex *t; //tail
} set;

int cmp(const void *a, const void *b)
{
	return (*(edge*)a).w - (*(edge*)b).w;
}

int min(int a,int b)
{
	return (a>b)?b:a;
}

void set_union(set *list, int p,int k)
{
//	puts("set_union ENTER\n");
	list[p].t->n = list[k].h;
	list[p].t = list[k].t;
	vertex *v = list[k].h;
	while(v != NULL)
	{
		//b->h = a->h;
		list[v->v].h = list[p].h;
		v = v->n;
	}
	
//	puts("set_union EXIT\n");
}

int main() {
	
	int n,m,i,j,p,k;
	set V[MAX_N+1];
	edge *E;
	vertex *v;
	scanf("%d %d",&n,&m);
	E=malloc(sizeof(edge)*(m+1));
	//V=malloc(sizeof(list)*(n+1));

	for(i=1;i<=n;i++)
	{
//		printf("init: %d\n",i);
		V[i].h=malloc(sizeof(vertex));
		V[i].h->n=NULL;
		V[i].h->v=i;
		V[i].t=V[i].h;
	}
	
	for(i=0;i<m;i++)scanf("%d %d %d",&E[i].p,&E[i].k,&E[i].w);
	
	qsort(E,m,sizeof(edge),cmp);
	for(i=0;i<m;i++)
	{
		p = E[i].p;
		k = E[i].k;
		printf("[%d]%d %d %d ",i,p,k,E[i].w);
		
		if(V[p].h->v != V[k].h->v)
		{
			if(V[p].h->v < V[k].h->v)
			{
				set_union(V,p, k);
			}
			else
			{
				set_union(V,k, p);
			}
			printf("TAK\n");
		}
		else
			printf("NIE\n");
	}


	for(i=1; i<=n; i++)
	{
		v=V[i].h;
		printf("[%d] ",i);
		while(v != NULL)
		{
			printf("%d ",v->v);
			v = v->n;
		}
		printf("\n");
	}

	return 0;
}
